package com.ludomancy.spaceopera;

import java.awt.Point;
import java.awt.Rectangle;

import com.ludomancy.gamelib.Randomizer;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.gamelib.WorldScreenProjection;
import com.ludomancy.gamelib.entity.World;
import com.ludomancy.spaceopera.entities.BlasterItem;
import com.ludomancy.spaceopera.entities.Leviathan;
import com.ludomancy.spaceopera.entities.PullerItem;
import com.ludomancy.spaceopera.entities.ScannerItem;
import com.ludomancy.spaceopera.entities.Star;
import com.ludomancy.spaceopera.entities.Station;

public class Game
{
	public static Game game;
	private SpaceWorld spaceWorld;
	public World currentWorld;
	public Player player;
	public Station station;
	public Leviathan leviathan;
	
	
	public static final int WORLD_WIDTH = 10000;
	public static final int WORLD_HEIGHT = 1764;
	public int screenWidth;
	public int screenHeight;

	private World worldToSwitch = null;
	private Point lastSpacePos = new Point();
	
	public WorldScreenProjection defaultProjector;
	public boolean paused = false;
	
	public Game(int width, int height)
	{
		game = this;
		spaceWorld = new SpaceWorld(WORLD_WIDTH, WORLD_HEIGHT);
		screenWidth = width;
		screenHeight = height;
		currentWorld = spaceWorld;
		
		defaultProjector = new WorldScreenProjection(width, height, Game.WORLD_WIDTH, Game.WORLD_HEIGHT);
		
		generateWorld();
	}
	
	private void generateWorld()
	{
		// fixed entities
		leviathan = new Leviathan(new Point());
		currentWorld.add(leviathan);
		leviathan.pos.setLocation(WORLD_WIDTH+leviathan.getWidth()/2, WORLD_HEIGHT/2);

		station = new Station(new Point());
		station.pos.setLocation(station.getWidth()/2 + 100,station.getHeight()/2 + 200);
		station.addItem(new ScannerItem());
		station.addItem(new PullerItem());
		station.addItem(new BlasterItem());
		currentWorld.add(station);
		
		player = new Player(new Point(10,10));
		currentWorld.add(player);
				
		// random entities
		// stars
		for (int i = 0; i < 45; i++)
		{
			Star toAdd = new Star(new Point(), Randomizer.getRandom(0,Star.CLIMATES-1), Star.PLANET_CLASS_GENERIC, Randomizer.getRandom(0, 1));
			currentWorld.moveToUnoccupiedRandomPlace(null, toAdd);
			currentWorld.push(toAdd);
		}
		
		// add the extra "special" stars
		// put them in a relatively safe point
		Rectangle safeArea = new Rectangle(1000,0, 6000, Game.WORLD_HEIGHT);
		
		for (int i = Star.PLANET_CLASS_GENERIC+1; i < Star.PLANET_CLASSES; i++)
		{
			Star special = new Star(new Point(), Randomizer.getRandom(0,Star.CLIMATES-1), i, Randomizer.getRandom(0, 1));
			currentWorld.moveToUnoccupiedRandomPlace(safeArea, special);
			currentWorld.push(special);			
		}		
	}
	
	public void update()
	{
		if(paused)
		{
			return;
		}
		
		for (int i = 0; i < 20; i++)
		{
			currentWorld.update();
		}
		
		if(currentWorld instanceof LandWorld)
		{
			for (int i = 0; i < 20; i++)
			{
				spaceWorld.update();
			}			
		}
		
		if(worldToSwitch != null)
		{
			currentWorld = worldToSwitch;
			worldToSwitch = null;
		}
	}
	
	public void switchToWorld(World newWorld)
	{
		if(currentWorld == spaceWorld)
		{
			lastSpacePos.setLocation(player.pos);
		}
		newWorld.add(player);
		worldToSwitch = newWorld;
		spaceWorld.remove(player);
	}
	
	public void backToSpace()
	{
		worldToSwitch = spaceWorld;
		spaceWorld.add(player);
		Sound.play("launch.wav");
	}

	public void landfall(LandWorld planet)
	{
		switchToWorld(planet);
		Sound.play("land2.wav");
	}
		
	public static int forTime(int realTime)
	{
		return realTime*16;
	}
	
	public void setPaused(boolean paused)
	{
		this.paused  = paused;
	}

	public void death()
	{
		switchToStation();
		player.shield = player.getMaxShield()/3;
	}

	public void switchToStation()
	{
		StationWorld ui = new StationWorld();
		switchToWorld(ui);
	}
}
